package com.lenovo.manufacture.module;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Module15 extends BaseActivity {

    @BindView(R.id.rv_module15)
    RecyclerView rvModule15;
    List<Map<String, String>> people;
    @BindView(R.id.tv_type)
    BootstrapLabel tvType;
    @BindView(R.id.tv_price)
    BootstrapLabel tvPrice;
    RvAdapter adapter;

    @Layout(value = R.layout.activity_module15, title = "人员解雇")
    protected void init() {
        async(apiService.getAllPerson(), data -> {
            people = data.getData();
            adapter = new RvAdapter();
            rvModule15.setLayoutManager(new LinearLayoutManager(this));
            rvModule15.setAdapter(adapter);
        });
    }

    @OnClick({R.id.tv_type, R.id.tv_price})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_type:
                sort(people, "type", tvType, "类型");
                break;
            case R.id.tv_price:
                sort(people, "price", tvPrice, "薪资");
                break;
        }
        adapter.notifyDataSetChanged();
    }

    class RvAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_module15, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            viewHolder.btnBuy.setOnClickListener(v -> {
                int position = viewHolder.getAdapterPosition();
                if (position != -1) {
                    notifyItemRemoved(position);
                    showMsg(people.get(position).get("personName") + "已被解雇");
                    people.remove(position);
                    //调用删除学生员工接口
                }
            });
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Map<String, String> person = people.get(position);
            holder.lv1.setText(person.get("personName"));
            String type = "工程师";
            switch (person.get("type")) {
                case "1":
                    type = "工人";
                    break;
                case "2":
                    type = "技术人员";
                    break;
                case "3":
                    type = "质检员";
                    break;
            }
            holder.lv2.setText(type);
            holder.lv3.setText(decimalFormat.format(Integer.valueOf(person.get("price"))));
        }

        @Override
        public int getItemCount() {
            return people.size();
        }
    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lv_1)
        TextView lv1;
        @BindView(R.id.lv_2)
        TextView lv2;
        @BindView(R.id.lv_3)
        TextView lv3;
        @BindView(R.id.btn_buy)
        BootstrapButton btnBuy;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}